package com.liming.product.controller;

import com.liming.common.pojo.dto.PageDTO;
import com.liming.common.pojo.dto.product.AddCategoryDTO;
import com.liming.common.pojo.dto.product.UpdateCategoryDTO;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.product.CategoryTreeVO;
import com.liming.common.pojo.vo.product.CategoryVO;
import com.liming.common.result.LMResult;
import com.liming.product.service.CategoryMircoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * @author liming
 * @date 2025/1/15
 */
@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "分类接口-Service")
@RequestMapping("/service/category")
public class CategoryMircoController {

    private final CategoryMircoService categoryMircoService;

    @Operation(summary = "分类树")
    @GetMapping
    public LMResult<List<CategoryTreeVO>> tree() {
        return LMResult.success(categoryMircoService.tree());
    }

    @Operation(summary = "详情")
    @GetMapping("{id}")
    public LMResult<CategoryVO> get(@PathVariable Long id) {
        return LMResult.success(categoryMircoService.get(id));
    }

    @Operation(summary = "列表 - 精选分类")
    @GetMapping("selected")
    public LMResult<List<CategoryVO>> selectedCategory() {
        return LMResult.success(categoryMircoService.selectedCategory());
    }

    @Operation(summary = "新增")
    @PostMapping
    public LMResult<Boolean> add(AddCategoryDTO addCategoryDTO) {
        return LMResult.success(categoryMircoService.add(addCategoryDTO));
    }

    @Operation(summary = "更新")
    @PutMapping
    public LMResult<Boolean> update(UpdateCategoryDTO updateCategoryDTO) {
        return LMResult.success(categoryMircoService.updateCategory(updateCategoryDTO));
    }

    @Operation(summary = "更新 - 商品精选")
    @PutMapping("selected/{id}")
    public LMResult<Boolean> updateSelected(@PathVariable Long id) {
        return LMResult.success(categoryMircoService.updateSelected(id));
    }

    @Operation(summary = "删除")
    @DeleteMapping("{id}")
    public LMResult<Boolean> delete(@PathVariable Long id) {
        return LMResult.success(categoryMircoService.removeById(id));
    }

    @Operation(summary = "获取该分类ID下的所有子分类ID和自身ID")
    @GetMapping("parentAndChildren/{categoryId}")
    public LMResult<Collection<Long>> parentAndChildren(@PathVariable Long categoryId) {
        return LMResult.success(categoryMircoService.parentAndChildren(categoryId));
    }

    @GetMapping("page")
    public LMResult<PageVO<CategoryVO>> page(PageDTO page) {
        return LMResult.success(categoryMircoService.myPage(page));
    }
}
